<?php
	
	session_start();
	
	require_once '../db/db.php';
	
	$error = false;
	
	// delete selected game
	if(isset($_POST['delete_game']) && $_POST['delete_game'] == true) {
	
		// sanitize game id
		if(filter_var($_POST['id'], FILTER_VALIDATE_INT)) {
		
			$id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT);
		
		}else{
		
			$message[] = '<p class="error">Cannot validate game.</p>';
			$error = true;
			
		}
		
		// use transaction to delete game and game_details info
		try {
		
			$db->beginTransaction();
			
			// delete game
			$db->exec("DELETE FROM games WHERE game_id = " . $id);
			// delete details
			$db->exec("DELETE FROM game_details WHERE game_id = " . $id);
			
			$db->commit();
			
			$message[] = '<p class="success">Successfully deleted your game.</p>';
		
		} catch(PDOException $e) {
		
			$db->rollBack();
			$message[] = '<p class="error">Could not delete game.</p>';
			$message[] = '<p class="notice">' . $e->getMessage() . '</p>';
			$error = true;
		
		}
	
	}
	
	if(isset($message)) foreach($message as $m) print $m;

?>